package com.team.controller;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.xml.internal.ws.api.Cancelable;
import com.team.entity.Announcement;
import com.team.entity.CapitalReport;
import com.team.entity.DutyOfficer;
import com.team.entity.Equipment;
import com.team.entity.RepairMan;
import com.team.service.AdminiStrationService;
import com.team.util.PeportPage;



/**
 * 管理模块controller
 * 里面包含添加值班人员、修改值班人员、报表等功能
 * @author 陈福桔
 *
 */
@Controller
public class AdminiStrationController {
  
	@Autowired
	private AdminiStrationService adminiStrationServiceImpl;
	
	/*
	 * 实现添加值班人员
	 */
	@RequestMapping("SaveDutyOfficer")
	public String demo(HttpServletRequest request,Model model,DutyOfficer dutyOfficer){
		DutyOfficer duty = new DutyOfficer();
		String dutyName= request.getParameter("dutyName");
		String telephone=  request.getParameter("telephone");
		String room= request.getParameter("room");
		String dutyTime = request.getParameter("dutyTime");
		duty.setDutyName(dutyName);
		duty.setDutyTime(dutyTime);
		duty.setRoom(room);
		duty.setTelephone(telephone);
		
		adminiStrationServiceImpl.savedutyOfficer(duty);
		
		return "menu_admin.jsp";
	}
	/*
	 * 查询所有值班人员
	 */
	@RequestMapping("finall")
	public String finall(Model model){
		 
		List<DutyOfficer> dutyOfficers=adminiStrationServiceImpl.listallDutyOfficer();
		model.addAttribute("dutyOfficers",dutyOfficers);
		return "listalldutyofficer.jsp";
		
	}
	/*
	 *保存维修人员信息 
	 */
	@RequestMapping("saveRepairMan")
	public String savereman(HttpServletRequest request,Model model){
		RepairMan repairMan =new RepairMan();
		String name= request.getParameter("name");
		String tel=  request.getParameter("telephone");
		String department= request.getParameter("department");
	    repairMan.setName(name);
	    repairMan.setTelephone(tel);
	    repairMan.setDepartment(department);
		adminiStrationServiceImpl.saveRepairMan(repairMan);
		
		return "menu_admin.jsp";
	}
	/*
	 * 列出所有值班人员信息
	 */
	@RequestMapping("listallreman")
	public String listreman(Model model){
		 List<RepairMan> repairMans=adminiStrationServiceImpl.listallRepairMan();
		 model.addAttribute("repairMans",repairMans);
		return "listallrepairmans.jsp";
		
	}
	/*
	 * 修改时查询单个维修人员
	 */
	@RequestMapping("finRepairMan")
	public String finRepairMan(Model model,HttpServletRequest request){
		 String ids=request.getParameter("id");
		 int id=Integer.parseInt(ids);
		RepairMan repairMan=adminiStrationServiceImpl.finRepairManById(id);
		model.addAttribute("repairMan",repairMan);
	
		return "repairmanintfo.jsp";
		
	}
	/*
	 * 执行修改
	 */
	@RequestMapping("updRepairMan")
	public String updRepairMan(Model model,RepairMan repairMan){
		adminiStrationServiceImpl.updRepairMan(repairMan);                                                                   
		
		return "listallreman";
		
	}
	/*
	 * 删除RepairMan
	 */
	@RequestMapping("delRepairMan")
	public String delRepairMan(Model model,HttpServletRequest request){
		 String ids=request.getParameter("id");
		 int id=Integer.parseInt(ids);
		 System.out.println(id);
		 RepairMan repairMan = new RepairMan();
		 repairMan.setId(id);
		adminiStrationServiceImpl.delRepairMan(repairMan);                                                                   
		
		return "listallreman";
}
	//保存富文本信息
	@RequestMapping("saveAnnouncement")
	public String saveAnnouncement(Model model,HttpServletRequest request){
		Calendar calendar = Calendar.getInstance(); 
		String content=request.getParameter("content"); 
		String title=request.getParameter("headline");
		int year = calendar.get(Calendar.YEAR);      
		int month = calendar.get(Calendar.MONTH);      
		int day = calendar.get(Calendar.DATE);
		int months=month+1;
		String time=year+"年"+months+"月"+day+"日";
		Announcement announcement = new Announcement();
		announcement.setReleaseTime(time);
		announcement.setAntitle(title);
		announcement.setContent(content);
		adminiStrationServiceImpl.saveAnnouncement(announcement);
		
		
		return "menu_admin.jsp";
	
	}
	@RequestMapping("listallAnnouncement")
	public String listallAnnouncement(Model model,HttpServletRequest request){
		List<Announcement> announcements=adminiStrationServiceImpl.listAllAnnouncement();
		model.addAttribute("announcements",announcements);                                                              
		
		return "listallannouncement.jsp";
	
      }
	//点击显示富文本详情
	@RequestMapping("selectAnnouncement")
	public String selectAnnouncement(Model model,HttpServletRequest request){
		String ids=request.getParameter("id");
		int id=Integer.parseInt(ids);
		Announcement announcement=adminiStrationServiceImpl.finAnnouncementById(id);
		model.addAttribute("announcement",announcement);                                                             
		return "selectannouncement.jsp";
	
      }
	//查询所有坏的机子
	@RequestMapping("selectEquipment")
	public String selectEquipmentBystatus(Model model,HttpServletRequest request){
		List<Equipment> equipments=adminiStrationServiceImpl.selectEquipmentBystatus();
		model.addAttribute("equipments",equipments);
		return "selectequpmenbystatus.jsp";
	
      }
	//点击维修完成后更改状态
	@RequestMapping("equipmentok")
	public String equipmentToOk(Model model,HttpServletRequest request){
		String ids = request.getParameter("id");
		int id=Integer.parseInt(ids);
		System.out.println("id"+id+"这个id 拿到是");
		adminiStrationServiceImpl.equipmentok(id);
		return "menu_admin.jsp";
	
      }
	//查询月报表
	@RequestMapping("selectMonthPeport")
	public String listMonthPeport(Model model,HttpServletRequest request,HttpSession session){
		  session=request.getSession();
			PeportPage  page2 =(PeportPage) session.getAttribute("page2");
			String months = request.getParameter("month");
			
			
			
	                       
			                 String a="shouye";
	                         String b="shangyiye";
	        				 String c="xiayiye";
			if ( page2==null||request.getParameter("status")==null) {
				 page2=new PeportPage();
			     page2.setPagenumber(0);
			     page2.setJijie(months);   
			}else
			   {
		    if(request.getParameter("status").equals(a)){
			    page2.setPagenumber(0);	 
		    }else if(request.getParameter("status").equals(b)){
			    if( page2.getPagenumber()!=0){
					 page2.setPagenumber( page2.getPagenumber()-1);	
		       }else{
		    	    page2.setPagenumber(0);
		    }	
		  }else if(request.getParameter("status").equals(c)){
			   page2.setPagenumber( page2.getPagenumber()+1);
			   int month=Integer.parseInt(page2.getJijie());
			   if(adminiStrationServiceImpl.listMonthPeport(month, page2.getPagenumber()).isEmpty()){
				   
				   List<CapitalReport> capitalReports = adminiStrationServiceImpl.listMonthPeport(month, page2.getPagenumber()-1);
				   page2.setPagenumber(page2.getPagenumber()-1);
				   
				   model.addAttribute("capitalReports",capitalReports);
				   
					return "findreports.jsp";
			   }
			   
			}
	}
		  session.setAttribute("page2", page2);
		  int month=Integer.parseInt(page2.getJijie());
	    List<CapitalReport> capitalReports=adminiStrationServiceImpl.listMonthPeport(month, page2.getPagenumber());
	    model.addAttribute("capitalReports",capitalReports);
		return "findreports.jsp";
	
      }
	//查询季度报表
	@RequestMapping("selectseasonPeport")
	public String selectseasonPeport(Model model,HttpServletRequest request,HttpSession session){
		   session=request.getSession();
		PeportPage  page =(PeportPage) session.getAttribute("page");
		String season = request.getParameter("season");
                       
		                 String a="shouye";
                         String b="shangyiye";
        				 String c="xiayiye";
		if ( page==null||request.getParameter("status")==null) {
			 page=new PeportPage();
		     page.setPagenumber(0);
		     page.setJijie(season);   
		}else
		   {
	    if(request.getParameter("status").equals(a)){
		    page.setPagenumber(0);	 
	    }else if(request.getParameter("status").equals(b)){
		    if( page.getPagenumber()!=0){
				 page.setPagenumber( page.getPagenumber()-1);	
	       }else{
	    	    page.setPagenumber(0);
	    }	
	  }else if(request.getParameter("status").equals(c)){
		   page.setPagenumber( page.getPagenumber()+1);
		}
}
		System.out.println(page.getPagenumber()+"页数");
		System.out.println(page.getJijie());
		session.setAttribute("page", page);
	    List<CapitalReport> capitalReports=adminiStrationServiceImpl.listSeasonPeport(page.getJijie(), page.getPagenumber());
	    model.addAttribute("capitalReports",capitalReports);
		return "findreportsbyseacon.jsp";
	
      }
	//查询年度报表
	@RequestMapping("selectyearPeport")
	public String selectyearPeport(Model model,HttpServletRequest request,HttpSession session){
		     session=request.getSession();
			PeportPage  page1 =(PeportPage) session.getAttribute("page1");
			String years = request.getParameter("year");
			
	                       
			                 String a="shouye";
	                         String b="shangyiye";
	        				 String c="xiayiye";
			if ( page1==null||request.getParameter("status")==null) {
				 page1=new PeportPage();
			     page1.setPagenumber(0);
			     page1.setJijie(years);   
			}else
			   {
		    if(request.getParameter("status").equals(a)){
			    page1.setPagenumber(0);	 
		    }else if(request.getParameter("status").equals(b)){
			    if( page1.getPagenumber()!=0){
					 page1.setPagenumber( page1.getPagenumber()-1);	
		       }else{
		    	    page1.setPagenumber(0);
		    }	
		  }else if(request.getParameter("status").equals(c)){
			   page1.setPagenumber( page1.getPagenumber()+1);
			}
	}
			System.out.println(page1.getPagenumber()+"页数");
			System.out.println(page1.getJijie());
		  session.setAttribute("page1", page1);
		 int year=Integer.parseInt(page1.getJijie());
	    List<CapitalReport> capitalReports=adminiStrationServiceImpl.listYearPeport(year, page1.getPagenumber());
	    model.addAttribute("capitalReports",capitalReports);
		return "findreportsbyyear.jsp";
	
      }
	
	
}
